//
//  TwoViewController.m
//  ceshi
//
//  Created by 乐辽科技 on 2018/6/14.
//  Copyright © 2018年 乐辽科技. All rights reserved.
//
#import "ceShiViewController.h"
#import "TwoViewController.h"
#import "OneViewController.h"
#import "ReactiveCocoa.h"//RAC响应式编程

@interface TwoViewController ()<OneViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *but;
@property (nonatomic ,strong) OneViewController *oneKvo;

@end

@implementation TwoViewController


//- (IBAction)tanchuang:(UIButton *)sender {
//    UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
////    vv.backgroundColor = [UIColor redColor];
//}
- (IBAction)up:(UIButton *)sender {
    OneViewController *one = [[OneViewController alloc] init];
    one.name = @"123456";
//    one.delegate = self;//代理
//
    //block
//    __weak typeof(self) weakSelf = self;
//    [one setData:^(NSString *textBlock) {
//        [self.but setTitle:textBlock forState:UIControlStateNormal];
//    }];
    
    //kvo单例
//    if (self.oneKvo == nil)
//    {
//        self.oneKvo = [[OneViewController alloc] init];
//        self.oneKvo.name = @"1231111";
//        //注册观察者
//
//        [[_oneKvo rac_valuesForKeyPath:@"dataText" observer:nil] subscribeNext:^(id x) {
//
//            [self.but setTitle:self.oneKvo.dataText forState:UIControlStateNormal];
//        }];// rac响应式编程 kvo
////        [self.oneKvo addObserver:self forKeyPath:@"dataText" options:NSKeyValueObservingOptionNew context:nil];
//    }
   
    [self.navigationController pushViewController:one animated:YES];
}
//kvo回调
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"dataText"])
    {
        [_but setTitle:self.oneKvo.dataText forState:UIControlStateNormal];
    }
    
    
}
//代理需要实现方法
-(void)textData:(NSString *)text
{
    [self.but setTitle:text forState:UIControlStateNormal];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"dataTextNotification" object:nil] subscribeNext:^(id  x) {
        NSNotification *xu = x;
        [self.but setTitle:xu.userInfo[@"dataTextNotification"] forState:UIControlStateNormal];
    }];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
